package com.globallogic.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.globallogic.bean.LoginBean;
import com.globallogic.consts.AppPages;

public class MyFilter implements Filter {
	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain filterChain) throws IOException, ServletException {
		
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		HttpServletRequest httpRequest = (HttpServletRequest) request;
		String requestURI = httpRequest.getRequestURI();

		HttpSession session = httpRequest.getSession(false);
		LoginBean loginBean = (session != null) ? (LoginBean) session
				.getAttribute("loginBean") : null;
				
        boolean isAdmin = (null != loginBean) ? loginBean.getIsLoggedUserAdmin() : false;
		
		if (requestURI.contains(AppPages.adminPart)) {

			if (!isAdmin) {
				httpResponse.sendRedirect(AppPages.accessDeny);
				return;
			}
		}

		filterChain.doFilter(request, response);
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
